package com.pxx.item.controller;

import com.pxx.item.pojo.Category;
import com.pxx.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/item/category/")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @RequestMapping("list")
    public ResponseEntity<List<Category>> queryCategoryListByParentId(@RequestParam(value = "pid",defaultValue = "0") Long pid){
        List<Category> list=categoryService.queryCategoryListByParentId(pid);
        if (CollectionUtils.isEmpty(list)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);
    }

    @PostMapping("add")
    public ResponseEntity<Void> add(@RequestBody Category category){
        return categoryService.add(category);
    }
    @PostMapping("edit")
    public ResponseEntity<Void> edit(@RequestBody Category category){
        return categoryService.edit(category);
    }

    @PostMapping("delete/{id}")
    public ResponseEntity<Void> delete(@PathVariable("id")Long id){
        return categoryService.delete(id);
    }



}
